package plus.xj17520.org.service.impl;

import plus.xj17520.basic.domain.BaseQuery;
import plus.xj17520.basic.util.PageList;
import plus.xj17520.org.domain.Employee;
import plus.xj17520.org.mapper.DepartmentMapper;
import plus.xj17520.org.mapper.EmployeeMapper;
import plus.xj17520.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//交给spring容器管理
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    DepartmentMapper departmentMapper;

    @Transactional
    @Override
    public void insert(Employee employee) {
        employeeMapper.insertSelective(employee);
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    @Override
    public PageList<Employee> findPage(BaseQuery baseQuery,String key) {
        PageList<Employee> page = employeeMapper.findPage(baseQuery, key);
        page.getList().stream().forEach(employee -> {
            employee.setDept(departmentMapper.loadById(employee.getDepartmentId()));
        });
        return page;
    }

    @Override
    public void delete(Long id) {
        employeeMapper.delete(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        employeeMapper.deleteBatch(ids);
    }

    @Override
    public List<Employee> findAll() {
        return employeeMapper.findAll();
    }

    @Override
    public Employee findOne(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }
}
